﻿using System;
using System.Threading;
using System.Threading.Tasks;
using SunnyTrader.Api;
using SunnyTrader.Common;

namespace SgdDemo
{
    class Stock
    {
        //static TradeApi_CTPFuture api = new TradeApi_CTPFuture();//ctp接口
        static BaseTradeApi api = new TradeApi_SgdFuturesExchange();//模拟交易所接口

        static void Main2(string[] args)
        {
            api.Connected += api_OnFrontConnect;
            api.DisConnected += api_OnDisConnected;
            api.RspUserLogin += api_RspUserLogin;
            api.RspQryTradingAccount += api_RspQryTradingAccount;
            api.RspQryInvestorPosition += api_RspQryInvestorPosition;
            api.RspQryOrder += api_RspQryOrder;
            api.RspQryTrade += api_RspQryTrade;
            api.RtnOrder += api_RtnOrder;
            api.RtnTrade += api_RtnTrade;
            api.RspOrderInsert += api_RspOrderInsert;
            api.RspOrderAction += api_RspOrderAction;
            //api.OnRspQryDepthMarketData += api_OnRspQryDepthMarketData;

            TradeApiLoginInforField loginInfo = new TradeApiLoginInforField();
            loginInfo.UserID = "f8";
            loginInfo.Password = "f8";
            loginInfo.CtpBrokerID = "8099";
            loginInfo.ServerAddr = "192.168.1.150:11003";
            loginInfo.GenericField = "";
            loginInfo.ApiType = EnumTradeApiType.期货模拟;
            api.Initialize(loginInfo);
            api.Connect();
            Console.ReadKey();
        }

        static void api_OnRspQryDepthMarketData(BaseTradeApi tApi, QuotaData arg2)
        {
            Console.WriteLine(Global.ObjectToJson(arg2));
        }

        static void api_RspOrderAction(BaseTradeApi tApi, OrderInforField arg1, ApiRspInfo arg2, int arg3, bool arg4)
        {
            Console.WriteLine("报单操作回报 OrderRef=" + arg1.OrderRef + ",InvestorID=" + arg1.InvestorID + ",LimitPrice=" + arg1.LimitPrice + ",ErrorID=" + arg2.ErrorID + ",ErrorMsg=" + arg2.ErrorMsg);
        }

        static void api_RspOrderInsert(BaseTradeApi tApi, InputOrderInforField arg1, ApiRspInfo arg2, int arg3, bool arg4)
        {
            Console.WriteLine("报单回报 OrderRef=" + arg1.OrderRef + ",InvestorID=" + arg1.InvestorID + ",LimitPrice=" + arg1.LimitPrice + ",ErrorID=" + arg2.ErrorID + ",ErrorMsg=" + arg2.ErrorMsg);
        }

        static void api_RtnTrade(BaseTradeApi tApi, TradeInforField obj)
        {
            Console.WriteLine("成交回报 code=" + obj.Code + ",Direction=" + obj.Direction + ",Offset=" + obj.OffsetFlag + ",HedgeFlag=" + obj.HedgeFlag + ",OrderSysID=" + obj.OrderSysID + ",Order_Ref=" + obj.OrderRef + ",Volume=" + obj.Volume + ",price=" + obj.Price + ",tradeid=" + obj.TradeID);
        }

        static void api_RtnOrder(BaseTradeApi tApi, OrderInforField obj)
        {
            //api.OrderAction(obj.Code, obj.FrontID, obj.SessionID, obj.OrderRef, obj.OrderSysID);
            Console.WriteLine("报单状态回报RtnOrder code=" + obj.Code + ",Direction=" + obj.Direction + ",Offset=" + obj.Offset + ",HedgeFlag=" + obj.HedgeFlag + ",sessionID=" + obj.SessionID + ",OrderSysID=" + obj.OrderSysID + ",LimitPrice=" + obj.LimitPrice + ",Order_Ref=" + obj.OrderRef + ",VolumeTotal=" + obj.VolumeTotal + ",VolumeTotalOriginal=" + obj.VolumeTotalOriginal + ",VolumeTraded=" + obj.VolumeTraded + ",status=" + obj.OrderStatus + ",statusmsg=" + obj.StatusMsg);
            // if (obj.OrderStatus == EnumOrderStatusType.NoTradeQueueing || obj.OrderStatus == EnumOrderStatusType.Unknown)
            //    api.OrderAction(obj.Code, g_frontID, obj.Direction.ToString(), obj.OrderRef, g_orderSysID);
        }

        static void api_RspQryTrade(BaseTradeApi tApi, TradeInforField obj, ApiRspInfo arg2, int arg3, bool arg4)
        {
            Console.WriteLine("api_RspQryTrade code=" + obj.Code + ",Direction=" + obj.Direction + ",Offset=" + obj.OffsetFlag + ",HedgeFlag=" + obj.HedgeFlag + ",OrderSysID=" + obj.OrderSysID + ",Order_Ref=" + obj.OrderRef + ",Volume=" + obj.Volume + ",price=" + obj.Price + ",tradeid=" + obj.TradeID + ",flag=" + arg4);
        }
        static void api_RspQryOrder(BaseTradeApi tApi, OrderInforField obj, ApiRspInfo arg2, int arg3, bool arg4)
        {
            Console.WriteLine("api_RspQryOrder code=" + obj.Code + ",frontID=" + obj.FrontID + ",Direction=" + obj.Direction + ",Offset=" + obj.Offset + ",HedgeFlag=" + obj.HedgeFlag + ",SessionID=" + obj.SessionID + ",price=" + obj.LimitPrice + ",order_ref=" + obj.OrderRef + ",OrderSysID=" + obj.OrderSysID + ",VolumeTotal=" + obj.VolumeTotal + ",VolumeTotalOriginal=" + obj.VolumeTotalOriginal + ",VolumeTraded=" + obj.VolumeTraded + ",status=" + obj.OrderStatus + ",statusmsg=" + obj.StatusMsg + ",flag=" + arg4);
        }

        static void api_RspQryMarketCodes(BaseTradeApi tApi, MarketCodeField arg1, ApiRspInfo arg2, int arg3, bool arg4)
        {
            Console.WriteLine("api_RspQryMarketCodes code=" + arg1.Code + ",name=" + arg1.CodeName + ",pricetick=" + arg1.PriceTick);
        }

        static void api_RspQryInvestorPosition(BaseTradeApi tApi, InvestorPositionField arg1, ApiRspInfo arg2, int arg3, bool arg4)
        {
            Console.WriteLine("持仓信息返回：Code=" + arg1.Code + ",Position=" + arg1.Position + ",PosiDirection=" + arg1.PosiDirection + ",YesterdayPosition=" + arg1.YesterdayPosition + ",StockAvailable=" + arg1.StockAvailable + ",TodayPosition=" + arg1.TodayPosition + ",flag=" + arg4);
        }

        static void api_RspQryTradingAccount(BaseTradeApi tApi, TradingAccountField arg1, ApiRspInfo arg2, int arg3, bool arg4)
        {
            Console.WriteLine("资金信息返回：PreBalance=" + arg1.PreBalance + ",Available=" + arg1.Available + ",Balance=" + arg1.Balance);
        }
        static void api_RspUserLogin(BaseTradeApi tApi, RspUserLoginField arg1, ApiRspInfo apiRspInf, int arg3, bool arg4)
        {
            Console.WriteLine(arg1.UserID + "登录返回1api_OnRspUserLogin errorID={0},msg={1},frontID={2},sessionID={3}, MaxOrderLocalID={4}", apiRspInf.ErrorID, apiRspInf.ErrorMsg, arg1.FrontID, arg1.SessionID, arg1.MaxOrderLocalID);
            if (apiRspInf.ErrorID == 0)
            {
                Task.Run(() =>
                {
                    Thread.Sleep(3000);

                    //测试发单
                    InputOrderInforField order = new InputOrderInforField();
                    order.Code = "rb2105";
                    order.Direction = EnumDirectionType.Buy;
                    order.HedgeFlag = EnumHedgeFlagType.Speculation;
                    order.LimitPrice = 4600;
                    order.OrderRef = "2";
                    order.VolumeTotalOriginal = 1;
                    order.Offset = EnumOffsetFlagType.Open;
                    string orderRef = api.OrderInsert(order);
                    //测试撤单,撤单需要订单信息
                    //api.OrderAction(obj.Code, obj.FrontID, obj.SessionID, obj.OrderRef, obj.OrderSysID);
                    Thread.Sleep(1500);
                    //测试查询资金信息
                    api.QryTradingAccount();
                    Thread.Sleep(1500);
                    //测试查询持仓信息
                    api.QryInvestorPosition();
                    Thread.Sleep(1500);
                    //测试查询订单信息
                    api.QryOrder();
                    Thread.Sleep(1500);
                    //测试查询成交信息
                    api.QryTrade();

                });
            }
        }
        /// <summary>
        /// 连接断开返回
        /// </summary>
        /// <param name="reason"></param>
        static void api_OnDisConnected(BaseTradeApi tApi, int reason)
        {
            Console.WriteLine("api_OnDisConnected");
        }
        /// <summary>
        /// 连接成功返回
        /// </summary>
        static void api_OnFrontConnect(BaseTradeApi tApi)
        {
            Console.WriteLine("api_OnFrontConnect1 success");
            Task.Run(() => { tApi.UserLogin(); });
        }
    }
}
